﻿using System;
using System.Text;
using System.Windows.Forms;

namespace dem2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int sesn = 0;
            int viStatus;
            int vi = 0;
            int retCount;
            byte[] bytes = new byte[20];//初始化
            viStatus = visa32.viOpenDefaultRM(out sesn);//打开VISA资源管理器句柄
            viStatus = visa32.viOpen(sesn, "TCPIP0::172.141.64.51::5025::SOCKET", visa32.VI_NO_LOCK, visa32.VI_TMO_IMMEDIATE, out vi);//打开仪器句柄

            viStatus = visa32.viPrintf(vi, "*RST\n");//复位指令
            viStatus = visa32.viPrintf(vi, ":SENS:BWID 1KHZ\n");//设置中频带宽为1KHZ
            viStatus = visa32.viPrintf(vi, ":SENS:FREQ:CENT 10000000\n");//键入中心频率

            //测量S12幅度迹线的标准差
            viStatus = visa32.viPrintf(vi, ":DISP:WIND:TRAC1:DEL\n");//删除轨迹
            viStatus = visa32.viPrintf(vi, ":CALCulate1:PARameter:DEF 'CH1',S12\n");//在通道1上创建测量名为‘CH1’，测量参数为S12的测量
            viStatus = visa32.viPrintf(vi, ":DISP:WIND:TRAC1:FEED  'CH1'\n");//创建轨迹1并将其与测量名称为‘CH1’的测量相关联，之后在窗口1中显示。
            viStatus = visa32.viPrintf(vi, ":CALCulate1:PARameter:SEL 'CH1'\n");//设置名称为‘CH1’的测量为通道1的当前选择测量。
            viStatus = visa32.viPrintf(vi, ":CALCulate1:FORMat  MLOGarithmic\n");//设置通道1当前选择轨迹的显示格式为对数幅度。
            viStatus = visa32.viPrintf(vi, ":CALCulate1:FUNCtion:STAT:STAT  ON\n");//显示通道1轨迹统计。
            viStatus = visa32.viPrintf(vi, ":CALCulate1:FUNCtion:TYPE  STDEV\n ");//设置通道1轨迹统计返回数据类型为标准偏差。
            System.Threading.Thread.Sleep(1000);//延时，给分析仪足够的时间进行计算
            viStatus = visa32.viPrintf(vi, ":CALCulate1:FUNCtion:DATA?\n");//返回通道1轨迹统计返回数据的数值
            viStatus = visa32.viRead(vi, bytes, 20, out retCount);//读取这个数值
            string idnStr1 = Encoding.ASCII.GetString(bytes, 0, bytes.Length);//byte[]转字符串

            //测量S12相位迹线的标准差
            viStatus = visa32.viPrintf(vi, ":CALCulate1:PARameter:DEF 'CH2',S12\n");
            viStatus = visa32.viPrintf(vi, ":DISP:WIND:TRAC2:FEED  'CH2'\n");
            viStatus = visa32.viPrintf(vi, ":CALCulate1:PARameter:SEL 'CH2'\n");
            viStatus = visa32.viPrintf(vi, ":CALCulate1:FORMat  PHASe\n");
            viStatus = visa32.viPrintf(vi, ":CALCulate1:FUNCtion:STAT:STAT  ON\n");
            viStatus = visa32.viPrintf(vi, ":CALCulate1:FUNCtion:TYPE  STDEV\n ");
            System.Threading.Thread.Sleep(1000);
            viStatus = visa32.viPrintf(vi, ":CALCulate1:FUNCtion:DATA?\n");
            viStatus = visa32.viRead(vi, bytes, 20, out retCount);
            string idnStr2 = Encoding.ASCII.GetString(bytes, 0, bytes.Length);//byte[]转字符串

            //测量S21幅度迹线的标准差
            viStatus = visa32.viPrintf(vi, ":CALCulate1:PARameter:DEF 'CH3',S21\n");
            viStatus = visa32.viPrintf(vi, ":DISP:WIND:TRAC3:FEED  'CH3'\n");
            viStatus = visa32.viPrintf(vi, ":CALCulate1:PARameter:SEL 'CH3'\n");
            viStatus = visa32.viPrintf(vi, ":CALCulate1:FORMat  MLOGarithmic\n");
            viStatus = visa32.viPrintf(vi, ":CALCulate1:FUNCtion:STAT:STAT  ON\n");
            viStatus = visa32.viPrintf(vi, ":CALCulate1:FUNCtion:TYPE  STDEV\n ");
            System.Threading.Thread.Sleep(1000);
            viStatus = visa32.viPrintf(vi, ":CALCulate1:FUNCtion:DATA?\n");
            viStatus = visa32.viRead(vi, bytes, 20, out retCount);
            string idnStr3 = Encoding.ASCII.GetString(bytes, 0, bytes.Length);//byte[]转字符串

            //测量S21相位迹线的标准差
            viStatus = visa32.viPrintf(vi, ":CALCulate1:PARameter:DEF 'CH4',S21\n");
            viStatus = visa32.viPrintf(vi, ":DISP:WIND:TRAC4:FEED  'CH4'\n");
            viStatus = visa32.viPrintf(vi, ":CALCulate1:PARameter:SEL 'CH4'\n");
            viStatus = visa32.viPrintf(vi, ":CALCulate1:FORMat  PHASe\n");
            viStatus = visa32.viPrintf(vi, ":CALCulate1:FUNCtion:STAT:STAT  ON\n");
            viStatus = visa32.viPrintf(vi, ":CALCulate1:FUNCtion:TYPE  STDEV\n ");
            System.Threading.Thread.Sleep(1000);
            viStatus = visa32.viPrintf(vi, ":CALCulate1:FUNCtion:DATA?\n");
            viStatus = visa32.viRead(vi, bytes, 20, out retCount);
            string idnStr4 = Encoding.ASCII.GetString(bytes, 0, bytes.Length);//byte[]转字符串

            textBox1.Multiline = true;//设置文本框的Multiline属性使其多行显示
            textBox1.Height = 60;//设置文本框的高
            textBox1.Text = idnStr1 +idnStr2 + idnStr3 + idnStr4;
            visa32.viClose(vi);//关闭VISA资源管理器
            visa32.viClose(sesn);//关闭仪器
        
    }
    }
}
